#include "videoform.h"
#include <QDesktopWidget>
#include <QMimeData>
#include <QMouseEvent>
#include <QPainter>
#include <QScreen>
#include <QShortcut>
#include <QStyle>
#include <QStyleOption>
#include <QTimer>
#include <QWindow>
#include <QThread>
#include <uibase/tipwidget.h>
#include <KWindowSystem>
#include <scrollsettingwidget.h>

#include "config.h"
#include "controller.h"
#include "basedevice.h"
#include "qyuvopenglwidget.h"
#include "ui_videoform.h"
#include "generatetools.h"

extern "C"
{
#include "libavutil/frame.h"
}

#ifndef UKUI_TASK_GSETTING_PATH
#define UKUI_TASK_GSETTING_PATH "org.ukui.panel.settings"
#endif

const int TITLE_HEIGHT = 40;
const int TRIPE_HEIGHT = 40;
const int ERROR_LENGTH = 1;

VideoForm::VideoForm(QWidget *parent, int screenIndex) : QWidget(parent), ui(new Ui::videoForm)
{
    m_screenIndex = screenIndex;
    if (m_screenIndex != 0) {
        QDesktopWidget *desktop = QApplication::desktop();
        this->setGeometry(desktop->screenGeometry(m_screenIndex));
    }
    ui->setupUi(this);
    initUI();
    this->setProperty("useStyleWindowManager", false);

    // 注册快捷键
    installShortcut();
    updateShowSize(size());

    // 注册事件过滤器
    this->installEventFilter(this);
    ui->keepRatioWidget->installEventFilter(this);
    ui->toolform->installEventFilter(this);
    ui->title->installEventFilter(this);

    initConnect();
    initPanelSetting();
}

VideoForm::~VideoForm()
{
    if (ui) {
        delete ui;
        ui = nullptr;
    }
}

void VideoForm::initUI()
{
    setAttribute(Qt::WA_DeleteOnClose);
    m_videoWidget = new QYUVOpenGLWidget();
    m_videoWidget->hide();

    m_loadingWidget = new LoadAnimation(this);
    m_loadingWidget->setHighLight();
    ui->keepRatioWidget->setWidget(m_loadingWidget);
    ui->keepRatioWidget->setWidget(m_videoWidget);
    ui->keepRatioWidget->setWidthHeightRatio(m_widthHeightRatio);
    ui->keepRatioWidget->setParent(this);

    this->setMouseTracking(true);
    m_videoWidget->setMouseTracking(true);
    ui->keepRatioWidget->setMouseTracking(true);
    m_titleHeight = TITLE_HEIGHT;
}

void VideoForm::initConnect()
{
    connect(ui->title, &VideoTitle::sigChangeNavigationStatus, this, &VideoForm::onToolFormStatusChange);
    connect(ui->title, &VideoTitle::sigMaxButtonClicked, this, &VideoForm::onMaxButtonClick);
    connect(ui->title, &VideoTitle::sigFullScreen, this, &VideoForm::onFullScreen);
    connect(ui->title, &VideoTitle::sigScroll, this, &VideoForm::onScrollTrigger);
}

void VideoForm::deleteUI()
{
    this->hide();
    if (!m_device) {
        return;
    }
    Config::getInstance().setRect(m_device->getSerial(), geometry());
}

QRect VideoForm::getGrabCursorRect()
{
    QRect rc;
    rc = QRect(ui->keepRatioWidget->mapToGlobal(m_videoWidget->pos()), m_videoWidget->size());
    rc.setTopLeft(rc.topLeft() * m_videoWidget->devicePixelRatio());
    rc.setBottomRight(rc.bottomRight() * m_videoWidget->devicePixelRatio());

    rc.setX(rc.x() + 10);
    rc.setY(rc.y() + 10);
    rc.setWidth(rc.width() - 20);
    rc.setHeight(rc.height() - 20);
    return rc;
}

const QSize &VideoForm::frameSize()
{
    return m_frameSize;
}

void VideoForm::updateRender(const AVFrame *frame)
{


    if (m_videoWidget->isHidden()) {
        this->show();
        updateShowSize(QSize(frame->width, frame->height));
        m_loadingWidget->resize(this->size());
        m_loadingWidget->show();
        GenerateTools::delayMs(1000);
        if (m_loadingWidget) {
            m_loadingWidget->close();
        }
        m_videoWidget->show();
    }
    updateShowSize(QSize(frame->width, frame->height));

    m_videoWidget->setFrameSize(QSize(frame->width, frame->height));
    m_videoWidget->updateTextures(frame->data[0], frame->data[1], frame->data[2], frame->linesize[0],
                                  frame->linesize[1], frame->linesize[2]);
}

QRect VideoForm::getScreenRect()
{
    QRect screenRect;
    QDesktopWidget *desktop = QApplication::desktop();
    if (m_screenIndex == 0) {
        screenRect = desktop->availableGeometry();
        if ((desktop->screenCount() > 1) && m_panelSetting) {
            int panelPosition = m_panelSetting->get("panelposition").toInt();
            int panelHeight = 0;
            int panelWidth = 0;
            switch (panelPosition) {
            case 0: { // 下
                panelHeight = m_panelSetting->get("panelsize").toInt();
                break;
            }
            case 1: { // 上
                panelHeight = m_panelSetting->get("panelsize").toInt();
                break;
            }
            case 2: { // 左
                panelWidth = m_panelSetting->get("panelsize").toInt();
                break;
            }
            case 3: { // 右
                panelWidth = m_panelSetting->get("panelsize").toInt();
                break;
            }
            default:
                break;
            }
            screenRect.setHeight(screenRect.height() - panelHeight);
            screenRect.setWidth(screenRect.width() - panelWidth);
        }
    } else {
        this->setGeometry(desktop->screenGeometry(m_screenIndex));
        QList<QScreen *> screenList = QGuiApplication::screens();
        screenRect = screenList[m_screenIndex]->geometry();
    }
    return screenRect;
}

void VideoForm::setScreenIndex()
{
    QDesktopWidget *desktop = QApplication::desktop();
    for (int i = 0; i < desktop->screenCount(); i++) {
        if (desktop->screenGeometry(i).contains(this->geometry())) {
            m_screenIndex = i;
            return;
        }
    }
}

void VideoForm::moveCenter()
{
    QRect screenRect = getScreenRect();

    if (screenRect.isEmpty()) {
        qWarning() << "getScreenRect is empty";
        return;
    }
    // 垂直窗口在最右侧，横屏时窗口居中
    if (m_isVertical) {
        this->move(screenRect.topRight() - QRect(0, 0, size().width(), size().height()).topRight());
    } else {
        this->move(screenRect.center() - QRect(0, 0, size().width(), size().height()).center());
    }
}

void VideoForm::installShortcut()
{
    QShortcut *shortcut = nullptr;

    // postGoHome
    shortcut = new QShortcut(QKeySequence("Ctrl+h"), this);
    shortcut->setAutoRepeat(false);
    connect(shortcut, &QShortcut::activated, this, [this]() {
        if (!m_device) {
            return;
        }
        Q_EMIT m_device->postGoHome();
    });

    // postGoBack
    shortcut = new QShortcut(QKeySequence("Ctrl+b"), this);
    shortcut->setAutoRepeat(false);
    connect(shortcut, &QShortcut::activated, this, [this]() {
        if (!m_device) {
            return;
        }
        Q_EMIT m_device->postGoBack();
    });

    // postAppSwitch
    shortcut = new QShortcut(QKeySequence("Ctrl+s"), this);
    shortcut->setAutoRepeat(false);
    connect(shortcut, &QShortcut::activated, this, [this]() {
        if (!m_device) {
            return;
        }
        Q_EMIT m_device->postAppSwitch();
    });

    // postGoMenu
    shortcut = new QShortcut(QKeySequence("Ctrl+m"), this);
    shortcut->setAutoRepeat(false);
    connect(shortcut, &QShortcut::activated, this, [this]() {
        if (!m_device) {
            return;
        }
        Q_EMIT m_device->postGoMenu();
    });

    // postVolumeUp
    shortcut = new QShortcut(QKeySequence("Ctrl+up"), this);
    connect(shortcut, &QShortcut::activated, this, [this]() {
        if (!m_device) {
            return;
        }
        Q_EMIT m_device->postVolumeUp();
    });

    // postVolumeDown
    shortcut = new QShortcut(QKeySequence("Ctrl+down"), this);
    connect(shortcut, &QShortcut::activated, this, [this]() {
        if (!m_device) {
            return;
        }
        Q_EMIT m_device->postVolumeDown();
    });

    // postPower
    shortcut = new QShortcut(QKeySequence("Ctrl+p"), this);
    shortcut->setAutoRepeat(false);
    connect(shortcut, &QShortcut::activated, this, [this]() {
        if (!m_device) {
            return;
        }
        Q_EMIT m_device->postPower();
    });

    // setScreenPowerMode(ControlMsg::SPM_OFF)
    shortcut = new QShortcut(QKeySequence("Ctrl+o"), this);
    shortcut->setAutoRepeat(false);
    connect(shortcut, &QShortcut::activated, this, [this]() {
        if (!m_device) {
            return;
        }
        Q_EMIT m_device->setScreenPowerMode(ControlMsg::SPM_OFF);
    });

    // expandNotificationPanel
    shortcut = new QShortcut(QKeySequence("Ctrl+n"), this);
    shortcut->setAutoRepeat(false);
    connect(shortcut, &QShortcut::activated, this, [this]() {
        if (!m_device) {
            return;
        }
        Q_EMIT m_device->expandNotificationPanel();
    });

    // collapsePanel
    shortcut = new QShortcut(QKeySequence("Ctrl+Shift+n"), this);
    shortcut->setAutoRepeat(false);
    connect(shortcut, &QShortcut::activated, this, [this]() {
        if (!m_device) {
            return;
        }
        Q_EMIT m_device->collapsePanel();
    });

    // copy
    shortcut = new QShortcut(QKeySequence("Ctrl+c"), this);
    shortcut->setAutoRepeat(false);
    connect(shortcut, &QShortcut::activated, this, [this]() {
        if (!m_device) {
            return;
        }
        Q_EMIT m_device->postCopy();
    });

    // cut
    shortcut = new QShortcut(QKeySequence("Ctrl+x"), this);
    shortcut->setAutoRepeat(false);
    connect(shortcut, &QShortcut::activated, this, [this]() {
        if (!m_device) {
            return;
        }
        Q_EMIT m_device->postCut();
    });

    // clipboardPaste
    shortcut = new QShortcut(QKeySequence("Ctrl+v"), this);
    shortcut->setAutoRepeat(false);
    connect(shortcut, &QShortcut::activated, this, [this]() {
        if (!m_device) {
            return;
        }
        Q_EMIT m_device->setDeviceClipboard();
    });

    // setDeviceClipboard
    shortcut = new QShortcut(QKeySequence("Ctrl+Shift+v"), this);
    shortcut->setAutoRepeat(false);
    connect(shortcut, &QShortcut::activated, this, [this]() {
        if (!m_device) {
            return;
        }
        Q_EMIT m_device->clipboardPaste();
    });

    // 全屏
    shortcut = new QShortcut(QKeySequence("F11"), this);
    shortcut->setAutoRepeat(false);
    connect(shortcut, &QShortcut::activated, this, [this]() {
        if (!m_device) {
            return;
        }
        Q_EMIT ui->title->onFullScreenTrigger();
    });
}

void VideoForm::updateShowSize(const QSize &newSize)
{
    m_widthHeightRatio = 1.0f * newSize.width() / newSize.height();
    ui->keepRatioWidget->setWidthHeightRatio(m_widthHeightRatio);
    m_isVertical = m_widthHeightRatio < 1.0f;
    if (m_frameSize != newSize) {
        m_frameSize = newSize;
        if (m_isFull) {
            ui->title->onFullScreenTrigger();
        }
        setWindowSize();
        moveCenter();
    }
}

void VideoForm::setDevice(BaseDevice *device)
{
    m_device = device;
    connect(ui->title, &VideoTitle::sigCloseWindow, m_device, &BaseDevice::sigVideoClose);
    ui->toolform->setDevice(m_device);
}

bool VideoForm::eventFilter(QObject *watch, QEvent *event)
{
    if (watch == this) {
        switch (event->type()) {
        case QEvent::InputMethod: {
            this->changeInputBoxPos();
            QInputMethodEvent *inputMethodEvent = (QInputMethodEvent *)event;
            if (!hasFocus()) { //失去焦点时，输入法事件优先于FocusOut事件被处理
                inputMethodEvent->setCommitString(""); //将失去焦点后提交的字符串设置为空
            } else {
                QString commitText = inputMethodEvent->commitString();
                if (!commitText.isEmpty()) {
                    qDebug() << "commitText = " << commitText;
                    Q_EMIT m_device->sigInputChinese(commitText);
                }
            }
            return true;
        }
        case QEvent::KeyPress: {
            QKeyEvent *keyEvent = (QKeyEvent *)event;
            if (keyEvent->key() == Qt::Key_Escape && m_isFull) {
                Q_EMIT ui->title->onFullScreenTrigger();
                return true;
            }
            if (!m_device) {
                return false;
            }
            Q_EMIT m_device->keyEvent(keyEvent, m_videoWidget->frameSize(), m_videoWidget->size());
            return true;
        }
        case QEvent::KeyRelease: {
            QKeyEvent *keyEvent = (QKeyEvent *)event;
            if (!m_device) {
                return false;
            }

            Q_EMIT m_device->keyEvent(keyEvent, m_videoWidget->frameSize(), m_videoWidget->size());
            return true;
        }
        case QEvent::Paint: {
            QStyleOption opt;
            opt.init(this);
            QPainter p(this);
            style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
            return true;
        }
        case QEvent::Close: {
            Q_EMIT m_device->sigVideoClose();
            return true;
        }
            //        case QEvent::DragEnter: {
            //            QDragEnterEvent *dragEnterEvent = (QDragEnterEvent *)event;
            //            dragEnterEvent->acceptProposedAction();
            //            return true;
            //        }
            //        case QEvent::Drop: {
            //            QDropEvent *dropEvent = (QDropEvent *)event;
            //            if (!m_device) {
            //                return false;
            //            }
            //            const QMimeData *qm = dropEvent->mimeData();
            //            QList<QUrl> urls = qm->urls();
            //
            //            for (const QUrl &url : urls) {
            //                QString file = url.toLocalFile();
            //                QFileInfo fileInfo(file);
            //
            //                if (!fileInfo.exists()) {
            //                    QMessageBox::warning(this, tr("mobile-assistant"), tr("file does not
            //                    exist"),
            //                                         QMessageBox::Ok);
            //                    continue;
            //                }
            //
            //                if (fileInfo.isFile() && fileInfo.suffix() == "apk") {
            //                    Q_EMIT m_device->installApkRequest(file);
            //                    continue;
            //                }
            //                Q_EMIT m_device->pushFileRequest(file, Config::getInstance().getPushFilePath() +
            //                fileInfo.fileName());
            //            }
            //
            //            return true;
            //        }
        case QEvent::MouseButtonPress: {
            QMouseEvent *mouseEvent = (QMouseEvent *)event;
            m_cursorPos = calCursorPos(mouseEvent->pos(), calCursorX(mouseEvent->pos()));
            if (mouseEvent->button() == Qt::LeftButton) {
                if (m_cursorPos != CENTER) {
                    resizeFlag = true;
                }
                m_preGeometry = geometry();
                m_viewMousePos = mouseEvent->globalPos();
            }
            return true;
        }
        case QEvent::MouseButtonRelease: {
            resizeFlag = false;
            return true;
        }
        case QEvent::MouseMove: {
            QMouseEvent *mouseEvent = (QMouseEvent *)event;
            if (!(m_isMax || m_isFull)) {
                setCursorShape(calCursorPos(mouseEvent->pos(), calCursorX(mouseEvent->pos())));
                windowZoom();
            }
            return true;
        }
        case QEvent::Resize: {
            if (m_toolForm) {
                m_toolForm->move(0, this->height() - 40);
                m_toolForm->setFixedWidth(this->width());
            }
        }
        default:
            break;
        }
    }
    if (watch == ui->title) {
        switch (event->type()) {
        case QEvent::Enter: {
            this->setCursor(Qt::ArrowCursor);
            return true;
        }
        case QEvent::MouseButtonPress: {
            QMouseEvent *mouseEvent = (QMouseEvent *)event;
            if (mouseEvent->buttons() == Qt::LeftButton) {
                m_isMousePress = true;
                m_startWindowPos = mouseEvent->globalPos();
            }
            return true;
        }
        case QEvent::MouseMove: {
            QMouseEvent *mouseEvent = (QMouseEvent *)event;
            // 自定义标题栏拖拽
            if (m_isMousePress && (!(m_isMax || m_isFull))) {
                this->setCursor(Qt::OpenHandCursor);
                this->move(this->geometry().topLeft() + mouseEvent->globalPos() - m_startWindowPos);
                m_startWindowPos = mouseEvent->globalPos();
            }
            return true;
        }
        case QEvent::MouseButtonRelease: {
            m_isMousePress = false;
            this->setCursor(Qt::ArrowCursor);
            return true;
        }
        default:
            break;
        }
    }
    if (watch == ui->keepRatioWidget) {
        switch (event->type()) {
        case QEvent::MouseButtonPress: {
            QMouseEvent *mouseEvent = (QMouseEvent *)event;
            if (mouseEvent->button() == Qt::MiddleButton) {
                if (m_device && !m_device->isCurrentCustomKeymap()) {
                    return false;
                }
            }
            if (mouseEvent->button() == Qt::RightButton) {
                if (m_device && !m_device->isCurrentCustomKeymap()) {
                    return false;
                }
            }

            if (m_videoWidget->geometry().contains(mouseEvent->pos())) {
                if (!m_device) {
                    return false;
                }
                mouseEvent->setLocalPos(m_videoWidget->mapFrom(ui->keepRatioWidget, mouseEvent->localPos().toPoint()));
                Q_EMIT m_device->mouseEvent(mouseEvent, m_videoWidget->frameSize(), m_videoWidget->size());
            } else {
                if (mouseEvent->button() == Qt::LeftButton) {
                    m_dragPosition = mouseEvent->globalPos() - frameGeometry().topLeft();
                    mouseEvent->accept();
                }
            }
            return true;
        }
        case QEvent::Enter: {
            this->setCursor(Qt::ArrowCursor);
            return true;
        }
        case QEvent::MouseMove: {
            QMouseEvent *mouseEvent = (QMouseEvent *)event;
            if (m_videoWidget->geometry().contains(mouseEvent->pos())) {
                if (!m_device) {
                    return false;
                }
                mouseEvent->setLocalPos(m_videoWidget->mapFrom(ui->keepRatioWidget, mouseEvent->localPos().toPoint()));
                Q_EMIT m_device->mouseEvent(mouseEvent, m_videoWidget->frameSize(), m_videoWidget->size());
            } else if (!m_dragPosition.isNull()) {
                if (mouseEvent->buttons() & Qt::LeftButton) {
                    move(mouseEvent->globalPos() - m_dragPosition);
                    mouseEvent->accept();
                }
            }
            return true;
        }
        case QEvent::MouseButtonRelease: {
            QMouseEvent *mouseEvent = (QMouseEvent *)event;
            if (m_dragPosition.isNull()) {
                if (!m_device) {
                    return false;
                }
                mouseEvent->setLocalPos(m_videoWidget->mapFrom(ui->keepRatioWidget, mouseEvent->localPos().toPoint()));
                // local check
                QPointF local = mouseEvent->localPos();
                if (local.x() < 0) {
                    local.setX(0);
                }
                if (local.x() > m_videoWidget->width()) {
                    local.setX(m_videoWidget->width());
                }
                if (local.y() < 0) {
                    local.setY(0);
                }
                if (local.y() > m_videoWidget->height()) {
                    local.setY(m_videoWidget->height());
                }
                mouseEvent->setLocalPos(local);
                Q_EMIT m_device->mouseEvent(mouseEvent, m_videoWidget->frameSize(), m_videoWidget->size());
            } else {
                m_dragPosition = QPoint(0, 0);
            }
            return true;
        }
        case QEvent::MouseButtonDblClick: {
            QMouseEvent *mouseEvent = (QMouseEvent *)event;
            if (m_videoWidget->geometry().contains(mouseEvent->pos())) {
                if (!m_device) {
                    return false;
                }
                mouseEvent->setLocalPos(m_videoWidget->mapFrom(ui->keepRatioWidget, mouseEvent->localPos().toPoint()));
                Q_EMIT m_device->mouseEvent(mouseEvent, m_videoWidget->frameSize(), m_videoWidget->size());
            }
            return true;
        }
        case QEvent::Wheel: {
            QMouseEvent *mouseEvent = (QMouseEvent *)event;
            if (m_videoWidget->geometry().contains(mouseEvent->pos())) {
                if (!m_device) {
                    return false;
                }
                QPoint globalPos = mouseEvent->globalPos();
                QPointF pos = m_videoWidget->mapFrom(ui->keepRatioWidget, mouseEvent->pos());
                QWheelEvent *wheelEvent1 = (QWheelEvent *)event;
                QWheelEvent wheelEvent(pos, globalPos, wheelEvent1->pixelDelta(), wheelEvent1->angleDelta(),
                                       wheelEvent1->buttons(), wheelEvent1->modifiers(), wheelEvent1->phase(),
                                       wheelEvent1->inverted());
                Q_EMIT m_device->wheelEvent(&wheelEvent, m_videoWidget->frameSize(), m_videoWidget->size());
            }
            return true;
        }
        default:
            break;
        }
    }
    if (watch == ui->toolform) {
        switch (event->type()) {
        case QEvent::Enter: {
            this->setCursor(Qt::ArrowCursor);
            return true;
        }
        default:
            break;
        }
    }
    return QWidget::eventFilter(watch, event);
}

void VideoForm::setCursorShape(int pos)
{
    Qt::CursorShape cursorShape;
    switch (pos) {
    case WindowEdge::TOP:
        cursorShape = Qt::SizeVerCursor;
        break;
    case WindowEdge::BUTTOM:
        cursorShape = Qt::SizeVerCursor;
        break;
    case WindowEdge::LEFT:
        cursorShape = Qt::SizeHorCursor;
        break;
    case WindowEdge::RIGHT:
        cursorShape = Qt::SizeHorCursor;
        break;
    case WindowEdge::BUTTOMLEFT:
        cursorShape = Qt::SizeBDiagCursor;
        break;
    case WindowEdge::BUTTOMRIGHT:
        cursorShape = Qt::SizeFDiagCursor;
        break;
    default:
        cursorShape = Qt::ArrowCursor;
        break;
    }
    this->setCursor(cursorShape);
}

void VideoForm::onGetInputState()
{
    qInfo() << "get chinese input state";
    this->setAttribute(Qt::WA_InputMethodEnabled, true);
    this->setFocusPolicy(Qt::WheelFocus);
    this->setFocus();
}

void VideoForm::onGetInputPos(int x, int y)
{
    m_inputX = x;
    m_inputY = y;
    changeInputBoxPos();
}

void VideoForm::onLostInputState()
{
    qInfo() << "lost chinese input state";
    this->setFocusPolicy(Qt::NoFocus);
    this->setAttribute(Qt::WA_InputMethodEnabled, false);
}

void VideoForm::changeInputBoxPos()
{
    if (testAttribute(Qt::WA_InputMethodEnabled)) {
        QTransform t;
        QPoint p = cursorPoint();
        t.translate(p.x() - this->width() / 2, p.y() - this->height()); //平移x,y
        QGuiApplication::inputMethod()->setInputItemTransform(t);
        QGuiApplication::inputMethod()->setInputItemRectangle(this->rect());
        QGuiApplication::inputMethod()->update(Qt::ImQueryInput);
    }
}

QPoint VideoForm::cursorPoint()
{
    QPoint point;
    point = this->mapTo(this->parentWidget(), QPoint(m_inputX * this->width() / m_frameSize.width(),
                                                     m_inputY * this->height() / m_frameSize.height()));

    point.setY(point.y() + 30);
    //如果输入坐标位置 + 50 > 屏幕分辨率端高度，此时输入法框会盖住输入框，需要进行如下处理，其中使用的50和150待优化
    const QDesktopWidget *desktop = QApplication::desktop();
    if (desktop) {
        QRect rect = desktop->screenGeometry();
        if (point.y() + 50 > rect.height()) {
            point.setY(point.y() - 200);
        }
    }

    return point;
}

int VideoForm::calCursorX(QPoint point)
{
    return (point.x() < frameShape ? 1 : ((point.x() > this->width() - frameShape) ? 3 : 2));
}

int VideoForm::calCursorPos(QPoint point, int cursorX)
{
    return ((point.y() < frameShape ? 10 : ((point.y() > this->height() - frameShape) ? 30 : 20)) + cursorX);
}

void VideoForm::windowZoom()
{
    QPoint currentPoint = QCursor::pos();            //获取当前的点，这个点是全局的
    QPoint moveSize = currentPoint - m_viewMousePos; //计算出移动的位置，当前点 - 鼠标左键按下的点
    QRect tempGeometry = m_preGeometry;              // 该值记录窗口缩放变化
    QScreen *screen = QGuiApplication::primaryScreen();
    QRect screenRect = screen->availableGeometry();
    int desktopHeight = QApplication::desktop()->availableGeometry().height();
    int desktopWidth = (desktopHeight - m_titleHeight) * m_widthHeightRatio;
    this->setMinimumSize(0, 0);
    this->setMaximumSize(desktopWidth, desktopHeight);
    if (resizeFlag) {
        switch (m_cursorPos) {
        case WindowEdge::LEFT:
            tempGeometry.setLeft(m_preGeometry.left() + moveSize.x());
            tempGeometry.setHeight((tempGeometry.width() / m_widthHeightRatio) + m_titleHeight);
            break;
        case WindowEdge::RIGHT:
            tempGeometry.setRight(m_preGeometry.right() + moveSize.x());
            tempGeometry.setHeight((tempGeometry.width() / m_widthHeightRatio) + m_titleHeight);
            break;
        case WindowEdge::BUTTOMLEFT:
            tempGeometry.setBottomLeft(m_preGeometry.bottomLeft() + moveSize);
            tempGeometry.setHeight((tempGeometry.width() / m_widthHeightRatio) + m_titleHeight);
            break;
        case WindowEdge::BUTTOM:
            tempGeometry.setBottom(m_preGeometry.bottom() + moveSize.y());
            tempGeometry.setWidth((tempGeometry.height() - m_titleHeight) * m_widthHeightRatio);
            break;
        case WindowEdge::BUTTOMRIGHT:
            tempGeometry.setBottomRight(m_preGeometry.bottomRight() + moveSize);
            tempGeometry.setWidth((tempGeometry.height() - m_titleHeight) * m_widthHeightRatio);
            break;
        default:
            break;
        }
        if (tempGeometry.height() >= screenRect.height()) {
            tempGeometry.setHeight(screenRect.height());
            tempGeometry.setWidth((tempGeometry.height() - m_titleHeight) * m_widthHeightRatio - ERROR_LENGTH);
            this->setGeometry(tempGeometry);
            this->resizeFlag = false;
        } else if (tempGeometry.height() <= (screenRect.height() / 2)) {
            tempGeometry.setHeight(screenRect.height() / 2);
            tempGeometry.setWidth((tempGeometry.height() - m_titleHeight) * m_widthHeightRatio + ERROR_LENGTH);
            this->setGeometry(tempGeometry);
            this->resizeFlag = false;
        }
        this->setGeometry(tempGeometry);
    }
    this->setFixedSize(this->width(), this->height());
}

void VideoForm::showToolForm(bool show)
{
    if (show) {
        m_isToolFormShow = true;
        m_titleHeight = TITLE_HEIGHT + TRIPE_HEIGHT;
        ui->toolform->setFixedHeight(40);
    } else {
        m_isToolFormShow = false;
        m_titleHeight = TITLE_HEIGHT;
        ui->toolform->setFixedHeight(0);
    }
}

void VideoForm::onToolFormStatusChange()
{
    if (m_device->getDeviceStatus()) {
        showToolForm(!m_isToolFormShow);
    } else {
        TipWidget *tipWidget = new TipWidget();
        tipWidget->setParent(this);
        tipWidget->SetMesseage(tr("Control device not supported"), nullptr, 20);
    }
}

void VideoForm::setWindowSize()
{
    QSize showSize = size();
    setScreenIndex();
    QRect screenRect = getScreenRect();
    int height = screenRect.height();
    int width = screenRect.width();
    if (m_isVertical) {
        showSize.setHeight(height);
        showSize.setWidth((height - m_titleHeight) * m_widthHeightRatio);
    } else {
        // 防止横屏后超出屏幕
        if ((width / m_widthHeightRatio) <= height) {
            showSize.setWidth(width);
            showSize.setHeight(width / m_widthHeightRatio + m_titleHeight);
        } else {
            showSize.setHeight(height);
            showSize.setWidth((height - m_titleHeight) * m_widthHeightRatio);
        }
    }
    setFixedSize(showSize);
}

void VideoForm::onMaxButtonClick(bool max)
{
    m_isMax = max;
    saveWindowConfig();
    setScreenIndex();

    QRect screenRect = getScreenRect();
    int height = screenRect.height();
    int width = screenRect.width();


    if (m_isMax) {
        if (m_beforeIsVertical == m_isVertical) {
            setFixedSize(m_beforeSize);
            move(m_beforePoint);
        } else {
            setWindowSize();
        }
    } else {
        if (m_isVertical) {
            this->setFixedSize((height - m_titleHeight) * m_widthHeightRatio, height);
            move(m_beforePoint.x(), 0);
        } else {
            this->setFixedSize(width, height);
        }
    }
    m_isMax = !m_isMax;
}

void VideoForm::onFullScreen()
{
    saveWindowConfig();
    setScreenIndex();

    QRect screenRect = getScreenRect();
    int height = QApplication::desktop()->screenGeometry().height();
    int width = QApplication::desktop()->screenGeometry().width();
    if (m_isFull) {
        KWindowSystem::clearState(this->winId(), KWindowSystem::KeepAbove);
        ui->title->show();
        if (m_isToolFormShow) {
            ui->toolform->show();
        }
        if (m_beforeIsVertical == m_isVertical) {
            setFixedSize(m_beforeSize);
            move(m_beforePoint);
        } else {
            setWindowSize();
        }
        m_isFull = false;
    } else {
        KWindowSystem::setState(this->winId(), KWindowSystem::KeepAbove);
        ui->title->hide();
        ui->toolform->hide();

        this->setFixedSize(width, height);
        moveCenter();
        m_isFull = true;
    }
}

void VideoForm::saveWindowConfig()
{
    if (!m_isFull && !m_isMax) {
        m_beforeIsVertical = m_isVertical;
        m_beforePoint = this->pos();
        m_beforeSize = this->size();
    }
}

void VideoForm::onScrollTrigger()
{
    ScrollSettingWidget *scrollWidget = new ScrollSettingWidget(this);
    scrollWidget->show();
}

void VideoForm::initPanelSetting()
{
    if (QGSettings::isSchemaInstalled(UKUI_TASK_GSETTING_PATH)) {
        qInfo() << "init panel gsettings";
        m_panelSetting = new QGSettings(UKUI_TASK_GSETTING_PATH);
    }
}